<template>
  <div class="login">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
      <!-- <h3 class="title">信息化关键基础设施统一管理平台</h3> -->
      <el-form-item prop="username">
        <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="账号">
          <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input v-model="loginForm.password" type="password" auto-complete="off" placeholder="密码"
          @keyup.enter.native="handleLogin">
          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item prop="code" v-if="captchaOnOff" class="mb25">
        <el-input v-model="loginForm.code" auto-complete="off" placeholder="验证码" style="width: 60%"
          @keyup.enter.native="handleLogin">
          <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
        </el-input>
        <div class="login-code">
          <img :src="codeUrl" @click="getCode" class="login-code-img" />
        </div>
      </el-form-item>
      <el-form-item class="operate-psd">
        <el-checkbox v-model="loginForm.rememberMe" class="checkbox">记住密码</el-checkbox>
        <el-button type="text" @click.native.prevent="resetPassword">忘记密码</el-button>
      </el-form-item>
      <el-form-item style="width: 100%">
        <el-button :loading="loading" size="medium" type="primary" style="width: 100%"
          @click.native.prevent="handleLogin">
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
        <div style="float: right" v-if="register">
          <router-link class="link-type" :to="'/register'">立即注册</router-link>
        </div>
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <!-- <div class="el-login-footer">
      <span>Copyright © 2018-2022 ruoyi.vip All Rights Reserved.</span>
    </div> -->
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";

export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
        code: "",
        uuid: "",
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" },
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" },
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }],
      },
      loading: false,
      // 验证码开关
      captchaOnOff: true,
      // 注册开关
      register: false,
      redirect: undefined,
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  created() {
    this.getCode();
    this.getCookie();
  },
  methods: {
    resetPassword() {
      this.$router.push({ path: "/resetPassword" });
    },
    getCode() {
      getCodeImg().then((res) => {
        let result = res.data;
        this.captchaOnOff =
          result.captchaOnOff === undefined ? true : result.captchaOnOff;
        if (this.captchaOnOff) {
          this.codeUrl = "data:image/gif;base64," + result.img;
          this.loginForm.uuid = result.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get("rememberMe");
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password:
          password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), {
              expires: 30,
            });
            Cookies.set("rememberMe", this.loginForm.rememberMe, {
              expires: 30,
            });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove("rememberMe");
          }
          this.$store
            .dispatch("Login", this.loginForm)
            .then(() => {
              // Cookies.set("navActive",1);
              // this.$store.dispatch("app/setNavActive", 1);
              // this.$nextTick(()=>{
              // this.getDefaultPath();
              // })
              this.$router.push({ path: "/" }).catch(() => { });
            })
            .catch(() => {
              this.loading = false;
              if (this.captchaOnOff) {
                this.loginForm.code = "";
                this.getCode();
              }
            });
        }
      });
    },
    /**获取默认首页地址*/
    getDefaultPath(index = 1, item) {
      console.log(this.$store.state.permission.sidebarRouters);
      let router = this.$store.state.permission.sidebarRouters;
      let baseSystem = [
        "资源管理",
        "数据可视化",
        "共享管理",
        "日志管理",
        "内部消息",
        "用户管理",
        "个人中心"
      ];
      const systemMap = new Map();
      systemMap.set(1, baseSystem);
      let result = [];
      if (index != 0) {
        result = router.filter((val) => {
          let arr = systemMap.get(parseInt(index));
          if (!val.hidden && val.path != "/") {
            return arr.includes(val.meta.title);
          }
        });
      }
      console.log(`${result[0].path}/${result[0].children[0].path}`);
    },
  },
};
</script>

<style rel="stylesheet/scss" lang="scss">
@function rem($args) {
  @return calc(100vh * #{$args} / 1080);
}

.login {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  height: 100%;
  background: url("../assets/images/login-background.jpg") center no-repeat;
  background-size: cover;
}

.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.login-form {
  position: relative;
  background: rgba(255, 255, 255, 0.05);
  width: rem(500);
  height: rem(612);
  padding: rem(160) rem(48) rem(25);
  margin-right: calc(100vw * 200 / 1920);

  &::before {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    width: rem(500);
    height: rem(612);
    background: url(../assets/images/login-border.png) no-repeat;
    background-size: 100% 100%;
  }

  .el-input {
    height: rem(56);
    width: 100%;

    input {
      height: rem(56);
    }
  }

  .el-input--medium .el-input__inner {
    line-height: rem(56);
  }

  .el-form-item--medium .el-form-item__content {
    line-height: rem(56);
  }

  .input-icon {
    height: rem(56);
    width: rem(20);
    margin-left: rem(20);
  }

  .el-input--medium .el-input__icon {
    line-height: rem(56);
  }

  .el-input--prefix .el-input__inner {
    padding-left: rem(62);
  }

  .el-form-item {
    margin-bottom: rem(32);

    &.mb25 {
      margin-bottom: rem(25);
    }
  }

  .el-input__inner {
    font-size: rem(20);
    color: #ffffff;
    border: rem(2) solid rgba(0, 75, 119, 0.6);
    border-radius: rem(6);

    &:focus,
    &:hover {
      border-color: rgba(0, 210, 255, 0.6);
    }
  }

  .el-checkbox__label {
    font-size: rem(16);
    color: #ffffff;
  }

  .el-checkbox__input.is-checked+.el-checkbox__label {
    color: #ffffff;
  }

  .el-checkbox__inner {
    width: rem(17);
    height: rem(17);
    border: rem(1) solid #00b8ff;
    border-radius: rem(3);
    background-color: transparent;

    &::after {
      height: rem(8);
      left: rem(5);
      top: rem(2);
      width: rem(4);
    }
  }

  .el-button--medium {
    padding: 0;
  }

  .el-button.el-button--primary {
    width: rem(404);
    height: rem(56);
    font-size: rem(26);
    line-height: rem(56);
    padding: 0;
    color: #ffffff;
    background: linear-gradient(0deg, #0177e4, #00b8ff);
    border-radius: rem(6);

    &:focus,
    &:hover {
      background: linear-gradient(0deg, #0177e4, #00b8ff);
    }
  }

  .checkbox {
    margin-bottom: rem(40);
    width: 70%;
  }
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.login-code {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  width: 38%;
  height: rem(56);
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

// .el-login-footer {
//   height: 40px;
//   line-height: 40px;
//   position: fixed;
//   bottom: 0;
//   width: 100%;
//   text-align: center;
//   color: #fff;
//   font-family: Arial;
//   font-size: 12px;
//   letter-spacing: 1px;
// }
.login-code-img {
  height: rem(52);
}

.operate-psd {
  height: 30px !important;
}
</style>
